package com.miaoxie.forum.module.auth.shared.utils;

import java.util.Map;

import com.alibaba.fastjson.JSONObject;

/**
 * JSON 字符串配创建工具类，示例：
 *  JSONBuilder.builder()
 *      .inflate("key","value")
 *      .inflate("hello","world")
 *      .build()
 *      .toString();
 */
public class JSONBuilder {
	private final JSONObject jsonObject;

    public static Builder builder() {
        return new Builder();
    }

    private JSONBuilder(final Builder builder) {
        this.jsonObject = builder.jsonObject;
    }


    public static class Builder {
        private JSONObject jsonObject = new JSONObject();

        public JSONBuilder build() {
            return new JSONBuilder(this);
        }

        public Builder inflate(String key, Object value) {
            jsonObject.put(key, value);
            return this;
        }

        public Builder inflateALL(Map map) {
            jsonObject.putAll(map);
            return this;
        }
    }

    @Override
    public String toString() {
        return jsonObject.toString();
    }
}
